package com.baosight.scaq.aq.ci.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.soa.SoaManager;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.scaq.common.aq.basetools.StaticTools;
import com.baosight.iplat4j.core.soa.SoaManager;

public class ServiceAQCI11 extends ServiceEPBase {
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
//		Dao dao = (Dao)SpringApplicationContext.getBean("dao");
//		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
//		//构造前台的EiBlock
//		EiBlock ei = new EiBlock("result");
//		ei.addBlockMeta(getMetadata());
//
//		HashMap<String, String> params = new HashMap<String, String>();
//		params.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
/*		List list = dao.query("ZMCIPersonInfo.queryCount",params);
		ei.addRows(list);
		inInfo.addBlock(ei);*/
		inInfo=this.queryAll(inInfo);
		return inInfo;
//		return SoaManager.call(inInfo);
	}
	
	public EiInfo query(EiInfo inInfo) {
//		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode").toString(), true);
		inInfo=super.query(inInfo, "AQCIRealtimePersonInfo.queryCount", getMetadata());
		return inInfo;
//		return SoaManager.call(inInfo);
	}
	
	
	public EiInfo queryAll(EiInfo inInfo)
	  {
	    EiInfo outInfo = new EiInfo();
//	    String project = FrameworkInfo.getProjectEname();
	    Map params = new HashMap();
	    String stmt = "AQCIRealtimePersonInfo.queryCount";
	    int count = this.dao.count(stmt, params);
	    int offset = 0;
	    int page = 1;
	    if(count > 1000) {
	    	page = count / 1000 + 1;
	    }
	    List ret = new ArrayList();
	    for(int i=0;i<page;i++) {
	    	ret.addAll( this.dao.query(stmt, params, offset, 1000) );
	    	offset += 1000;
	    }
	    
	    //List ret = this.dao.query(stmt, params);
	    for (int i = 0; i < ret.size(); i++) {
	      Map node = (Map)ret.get(i);
//	      node.put("label",i);
//	      String blockName = (String)node.get("fdType");
	      
	      String blockName1 = ((String)node.get("parent")).replace(" ", "");	  
	      String blockName = blockName1.replace("+", "");
//	      String blockName = (String)node.get("parent");	       	   
	      if (("root".equals(blockName))) {
	        blockName = "$";
	      }

	      EiBlock block = outInfo.getBlock(blockName);
	      if (block == null) {
	        block = outInfo.addBlock(blockName);
	        block.addBlockMeta(getMetadata());
	        outInfo.addBlock(block);
	      }
	      String label1 = ((String)node.get("label")).replace(" ", "");	  
	      String label2 = label1.replace("+", "");	  
	      node.put("parent", blockName);
	      node.put("label",label2);
	      block.addRow(node);

	      String leaf = node.get("leaf").toString();
	      if ("0".equals(leaf)) {	    	 
//	        String label1 = (String)node.get("fdMiningName");
//	        String label2 = (String)node.get("fdDepartment");
	    	
	        String label=(String)node.get("label");
//	        if(null==label2 || label2.equals("")){
//	        	if(label1.equals("公司井下人员合计")){
//	        		label1="公司";
//	        	}
//	        	label = label1;
//	        }else{
////	        	label = label1+label2.replace(" ", "");
//	        	label = label1+label2;
//	        }
	       
	       
	        EiBlock b = outInfo.getBlock(label);
	        if (b == null) {
	          b = outInfo.addBlock(label);
	          b.addBlockMeta(getMetadata());
	          outInfo.addBlock(b);
	        
//	        }
	      }	
	      }
	    }
	    return outInfo;
	  }
	
	
	
	
	
	private EiBlockMeta getMetadata()
	{
		EiBlockMeta eiMetadata = new EiBlockMeta();
		EiColumn eiColumn;

		eiColumn = new EiColumn("fdMiningName");
		eiColumn.setFieldLength(64);	
		eiColumn.setDescName("矿井名称");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdDepartment");
		eiColumn.setFieldLength(200);	
		eiColumn.setDescName("部门名称");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdLocationPlace");
		eiColumn.setFieldLength(50);	
		eiColumn.setDescName("所在位置");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("localNum");
		eiColumn.setFieldLength(64);	
		eiColumn.setDescName("人次");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdType");
		eiColumn.setFieldLength(64);	
		eiColumn.setDescName("类型");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("id");
		eiColumn.setFieldLength(64);	
		eiColumn.setDescName("id");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("leaf");
		eiColumn.setFieldLength(64);	
		eiColumn.setDescName("是否为叶子节点");
		eiMetadata.addMeta(eiColumn);



		return eiMetadata;
	}
}
